//单例-懒汉模式
//使用时创建实例
class SingletonLazy{
    private static Object locked = new Object();
    //防止instance被优化，和指令重排序
    private static volatile SingletonLazy instance = null;
    public static SingletonLazy getInstance(){
        if(instance == null) {
            synchronized (locked) {
                if(instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
    //私有的构造方法
    private SingletonLazy() {

    }
}
public class Singleton2 {
    public static void main(String[] args){
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1==s2);
    }
}
